package com.cefa.edu.controller;

import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.dao.ClassMapper;
import com.cefa.edu.model.EducationLevel;
import com.cefa.edu.service.ClassService;
import com.cefa.edu.service.UserOrganRoleService;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by cc664 on 2018/8/14.
 * @aa 班级controller
 */
@RestController
public class ClassController {
 Logger logger= LoggerFactory.getLogger(ClassController.class);
    /**
     * @aa 添加班级
     * @aa add by chaijian
     * @aa 2018-08-15
     */

    @Autowired
    ClassService classService;
    @Autowired
    UserOrganRoleService userOrganRoleService;
    @Autowired
    ClassMapper classMapper;

    /**
     * @aa 根据班级id 删除对应的班级数据
     * @aa add by chaijian 2018-08-15
     * @return
     */
    @RequestMapping("/education/deleteClass")
    public Result<Map<String,Object>> deleteClass(@Param("classId") Integer classId) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("falt",true);
        try {

            //校验参数是否为空
            if(classId==null ||classId==0){
                result.put("falt",false);
                result.put("msg","参数classId不可为空！");
            }else {
                //判断将删除的班级 是否有老师和学生 若该班有老师或者学生不可删除
                result=isHaveTeacherOrStudent(result,classId);
                if(null==result.get("msg")){
                    //删除班级
                    classService.deleteClass(classId);

                }else {
                    result.put("falt",false);
                }

            }

        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
            result.put("msg",e.getMessage());
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    //判断将删除的班级 是否有老师和学生 若该班有老师或者学生不可删除
    private Map<String,Object> isHaveTeacherOrStudent(Map<String,Object> result,Integer classId) {
       int count= userOrganRoleService.countUserOrganRoleByClassIdAndRoleType(classId);
       if(count>0){
           result.put("msg","该班级含有老师或学生数据，不可删除！");
       }
       return result;
    }

    /**
     * @aa 添加班级
     * @aa add by chaijian 2018-08-15
     * @return
     */
    @RequestMapping("/education/saveClass")
    public Result<Map<String,Object>> saveClass(Integer schoolId , Integer phaseId, Integer gradeId, String className) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("falt",true);
        try {

           //校验参数是否为空
            result=validateParamsNotNull(result,schoolId,phaseId,gradeId,className);
            if(null==result.get("msg")){
                classService.saveClass(schoolId,phaseId,gradeId,className);
            }else {
                //校验失败
                result.put("falt",false);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    //校验参数是否为空
    private Map<String,Object> validateParamsNotNull(Map<String,Object> result,Integer schoolId, Integer phaseId, Integer gradeId, String className) {
        if(schoolId==null ||schoolId==0){
            result.put("msg","学校Id不可为空！");
        } else if(gradeId==null||gradeId==0){
            result.put("msg","年级Id不可为空！");
        }else if(phaseId==null||phaseId==0){
            result.put("msg","科目名称不可为空！");
        }else if(Strings.isNullOrEmpty(className)){
            result.put("msg","记录名称不可为空！");
        }
        return  result;
    }

    @RequestMapping(path="/education/updateClass", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> updateClass(Integer classId,String className){
        Map<String, Object> result = Maps.newHashMap();
        try {
            classMapper.updateClass(classId,className);
            result.put("falt",true);
        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }
}
